package com.xjscrm.console.service.customer.wwCustomer.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xjscrm.common.entity.CustomerBehavior;
import com.xjscrm.console.mapper.CustomerBehaviorMapper;
import com.xjscrm.console.param.wwCustomer.CustomerBehaviorQuery;
import com.xjscrm.console.service.customer.wwCustomer.CustomerBehaviorService;
import com.xjscrm.console.vo.CustomerBehaviorVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CustomerBehaviorServiceImpl implements CustomerBehaviorService {

    @Autowired
    private CustomerBehaviorMapper customerBehaviorMapper;


    public Page<CustomerBehaviorVo> findByPage(CustomerBehaviorQuery customerBehaviorQuery) {

        //新建返回到页面的LIST对象
        List<CustomerBehaviorVo> customerBehaviorVoList = new ArrayList<>();

        //执行分页查询对象
        Page<CustomerBehavior> page = new Page<>();
        customerBehaviorQuery.convert(page);

        if(StringUtils.isBlank(customerBehaviorQuery.getOrderByField())){
            customerBehaviorQuery.setOrderByField("create_time");
        }

        List<CustomerBehavior> customerBehaviorList = customerBehaviorMapper.findByList(page, customerBehaviorQuery);
        if (customerBehaviorList != null) {
            for (CustomerBehavior customerBehavior : customerBehaviorList) {
                CustomerBehaviorVo customerBehaviorVo = new CustomerBehaviorVo();
                BeanUtils.copyProperties(customerBehavior, customerBehaviorVo);
                customerBehaviorVoList.add(customerBehaviorVo);

            }
        }

        //返回到页面分页对象
        Page<CustomerBehaviorVo> pageVo = new Page<>();
        BeanUtils.copyProperties(page, pageVo);
        pageVo.setRecords(customerBehaviorVoList);
        //将分页信息复制到返回到页面的分页MO当中
        return pageVo;

    }
}
